package com.qfedu.dao.impl;

import com.qfedu.dao.IAddressDao;
import com.qfedu.entity.Address;
import com.qfedu.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class AddressDaoImpl implements IAddressDao {
    @Override
    public int insertAddress(int uid, String name, String phone, String detail) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            int update = queryRunner.update(JDBCUtil.getConnection(), "insert into address(uid,name,phone,detail,level) values(?,?,?,?,?)", uid, name, phone, detail,0);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteAddress(int id) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            int update = queryRunner.update(JDBCUtil.getConnection(), "delete from address where id = ?", id);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateAddress(int id,String name, String phone, String detail) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            int update = queryRunner.update(JDBCUtil.getConnection(), "update address set name = ?,phone = ?,detail = ? where id = ?", name, phone, detail, id);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public Address queryAddress(int id) {
        try {
            return new QueryRunner(JDBCUtil.getDataSource()).query("select uid,name,phone,detail,level from address where id = ?",new BeanHandler<Address>(Address.class),id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Address> queryAddressList(int uid) {
        try {
            return new QueryRunner(JDBCUtil.getDataSource()).query("select id,name,phone,detail,level from address where uid = ?",new BeanListHandler<Address>(Address.class),uid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int setAddressOne(int id) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            int update = queryRunner.update(JDBCUtil.getConnection(), "update address set level = ? where id = ?", 1, id);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int setAddressZero(int id) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            int update = queryRunner.update(JDBCUtil.getConnection(), "update address set level = ? where id = ?", 0, id);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int queryDefaultAddress(int uid, int level){
        QueryRunner queryRunner = new QueryRunner();
        try {
            int update = queryRunner.update(JDBCUtil.getConnection(), "update address set level = ? where uid =? ", level, uid);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
}
